package entity.humans;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.LinkedList;

import user.Button;
import levels.PathFinder;
import levels.PathGrid;
import loading.ImageLoader;
import main.GameList;
import math.Vector2D;
import entity.building.Building;
import entity.units.AirUnit;
import entity.units.Unit;

public class HumanCenter extends Building
{
	private static final ImageLoader baseHuman = new ImageLoader( "Images/baseHuman.png" );
	private static final ImageLoader builderButton = new ImageLoader( "Images/builderButton.png" );
	private static final ImageLoader marineButton = new ImageLoader( "Images/marineButton.png" );
	private static final ImageLoader tankButton = new ImageLoader( "Images/tankButton.png" );
	private static final ImageLoader helicopterButton = new ImageLoader( "Images/helicopterButton.png" );
	
	private static final Button builder = new Button( new Vector2D( 824, 648 ), builderButton );
	private static final Button marine = new Button( new Vector2D( 864, 648 ), marineButton );
	private static final Button helicopter = new Button( new Vector2D( 904, 648 ), helicopterButton );
	private static final Button tank = new Button( new Vector2D( 944, 648 ), tankButton );

	public HumanCenter( Vector2D pos, PathGrid pg )
	{
		super( pos, baseHuman, pg, 20, 400 );

		super.addOption( builder );
		super.addOption( marine );
		super.addOption( helicopter );
		super.addOption( tank );
	}
	
	public boolean testOptions( Vector2D mouse, GameList list, PathFinder pf )
	{
		if( !this.isBuilt() )
			return false;
		
		if( builder.click( mouse) )
		{
			Builder au = new Builder( this.getTopLeftCorner().subtract( new Vector2D( 1, 1 ) ), pf );
			list.add( au );
			this.train( au );
			
			return true;
		}
		else if( marine.click( mouse ) )
		{
			Marine au = new Marine( this.getTopLeftCorner().subtract( new Vector2D( 1, 1 ) ), pf );
			list.add( au );
			this.train( au );
			
			return true;
		}
		else if( tank.click( mouse) )
		{
			Tank au = new Tank( this.getTopLeftCorner().subtract( new Vector2D( 1, 1 ) ), pf );
			list.add( au );
			this.train( au );
			
			return true;
		}
		else if( helicopter.click( mouse ) )
		{
			Helicopter au = new Helicopter( this.getTopLeftCorner().subtract( new Vector2D( 1, 1 ) ) );
			list.add( au );
			this.train( au );
			
			return true;
		}
		
		return false;
	}

	@Override
	public void drawInformation( Graphics2D g )
	{
		g.setTransform( AffineTransform.getTranslateInstance( 8 * 17, 648 ) );
		
		g.drawString( "Base", 20, 20 );
		
		if( !isBuilt() )
		{
			g.setColor( new Color( 24, 134, 195 ) );
			g.drawRect( 20, 40, 200, 20 );
			g.fillRect( 20, 40, (int)( 200 * getTimeBuilding() / getTimetoBuild() ), 20 );
		}
		
		LinkedList<Unit> queue = getQueue();
		if( !queue.isEmpty() )
		{
			Unit u;
			for( int i = 0; i < queue.size() && i < 5; i++ )
			{
				u = queue.get( i );
				
				if( u instanceof Builder )
					builderButton.draw( g, AffineTransform.getTranslateInstance( i * 48 + 20, 80  ) );
				else if( u instanceof Marine )
					marineButton.draw( g, AffineTransform.getTranslateInstance( i * 48 + 20, 80  ) );
				else if( u instanceof Tank )
					tankButton.draw( g, AffineTransform.getTranslateInstance( i * 48 + 20, 80  ) );
				else if( u instanceof Helicopter )
					helicopterButton.draw( g, AffineTransform.getTranslateInstance( i * 48 + 20, 80  ) );
			}
			
			g.setColor( new Color( 24, 134, 195 ) );
			g.drawRect( 20, 40, 200, 20 );
			g.fillRect( 20, 40, (int)( 200 * queue.get( 0 ).getTimeTraining() / queue.get( 0 ).getTimetoTrain() ), 20 );
		}
		
		g.setTransform( AffineTransform.getTranslateInstance( 0, 0 ) );
	}
}
